//
//  STTimerManagerView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/12.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STTimerManagerView.h"
#import "STTimerDayCell.h"
#import "UIButton+ZYHblock.h"

@implementation STTimerManagerView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    isEnable = 1;
    return self;
}

-(void)setMyTimerManagerViewWithTimerData:(NSDictionary *)timerDic{
    self.serviceTimerDic = timerDic;
    [self loadTimerManagerListView];
    [self loadSureBtn];
}



-(void)setServiceDayType:(TimerSelDayType)dayType WithDic:(NSDictionary *)dic{
    if (dayType == ServiceTimer) {
        _serviceTimerDic = dic;
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        [timerTableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
    }
}

-(void)setUnEnable:(BOOL)enable{
    isEnable =enable;
    [timerTableView reloadSection:1 withRowAnimation:UITableViewRowAnimationNone];
}

-(void)loadTimerManagerListView{
    arrTimerDays = @[@{@"name":@"设置周期"}];
    
    timerTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190) style:UITableViewStylePlain];
    timerTableView.delegate = self;
    timerTableView.dataSource = self;
    timerTableView.scrollEnabled = NO;
    timerTableView.separatorInset = UIEdgeInsetsZero;
    timerTableView.backgroundColor = [UIColor clearColor];
    timerTableView.tableFooterView = [UIView new];
    [self addSubview:timerTableView];
}

-(void)loadSureBtn{
    surBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-64-50, SCREEN_WIDTH, 50)];
    surBtn.backgroundColor = ButtonBackColor;
    [surBtn setTitle:@"保存" forState:UIControlStateNormal];
    WeakSelf
    surBtn.didSelectBtn = ^(UIButton * sender) {
        if ([weakSelf.delegate respondsToSelector:@selector(timerMagSaveTimer:)]) {
            [weakSelf.delegate timerMagSaveTimer:nil];
        }
    };
    [self addSubview:surBtn];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section ==0){
        return 1;
    }
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section) {
        return 10;
    }
    return 0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = @"imgDetailCell";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:identifier];
    if(indexPath.section == 0 && indexPath.row == 0){
        
        static NSString *identifier=@"STSwitchCell";
        cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell= [[[NSBundle mainBundle]loadNibNamed:@"STSwitchCell" owner:nil options:nil] firstObject];
        }
        UILabel * nameLab = [cell viewWithTag:10311];
        nameLab.text = @"服务开关";
        UISwitch * onSwitch = [cell viewWithTag:10312];
        [onSwitch addTarget:self action:@selector(actionChangeOpen:) forControlEvents:UIControlEventValueChanged];
        onSwitch.on =NO;
        return cell;
    }else if(indexPath.section ==1){
        static NSString * advIdentifier = @"advIdentifier";
        cell = [tableView dequeueReusableCellWithIdentifier:advIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:advIdentifier];
        }
        [cell.contentView  removeAllSubviews];
        cell.textLabel.text = @"";
        cell.detailTextLabel.text = @"";
        if(indexPath.row == 0){
            cell.textLabel.text = @"服务时间";
            cell.detailTextLabel.text = [self getTimerStr:ServiceTimer];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else if(indexPath.row == 1){
            cell.textLabel.text = @"服务周期";
            cell.detailTextLabel.text = @"";
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, SCREEN_WIDTH);
        }else if(indexPath.row == 2){
            
            UIView *collectionView = [self loadCollectionView];
            [cell.contentView addSubview:collectionView];
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, SCREEN_WIDTH);
        }
        if(isEnable){
            cell.textLabel.textColor = [UIColor blackColor];
        }else{
            cell.textLabel.textColor = [UIColor lightGrayColor];
        }
    }
        
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0 && indexPath.section == 1&&isEnable){
        if ([self.delegate respondsToSelector:@selector(timerMagSelTimer)]) {
            [self.delegate timerMagSelTimer];
        }
    }
}

-(void)actionChangeOpen:(UISwitch*)sender{
    if ([self.delegate respondsToSelector:@selector(timerMagOpenTheDoor:)]) {
        [self.delegate timerMagOpenTheDoor:sender];
    }
}

-(NSString*)getTimerStr:(TimerSelDayType)dayType{
    if (dayType == ServiceTimer) {
        if(!_serviceTimerDic){
             return @"";
        }
        NSString * serviceTimerStr = [NSString stringWithFormat:@"%@:%@-%@:%@",_serviceTimerDic[@"fromHour"],_serviceTimerDic[@"fromMinute"],_serviceTimerDic[@"endHour"],_serviceTimerDic[@"endMinute"]];
        return serviceTimerStr;
    }
    return @"";
}


-(UICollectionView *)loadCollectionView{
    float w = (SCREEN_WIDTH-(100)) / 3;
    float h = 30;
    
    UICollectionViewFlowLayout * flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize=CGSizeMake(w, h);
    flow.sectionInset=UIEdgeInsetsMake(0,10,0,10);
    UICollectionView * timerCollection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) collectionViewLayout:flow];
    [timerCollection registerClass:[STTimerDayCell class] forCellWithReuseIdentifier:@"timerDayCell"];
    timerCollection.delegate = self;
    timerCollection.dataSource = self;
    //    timerCollection.scrollEnabled=NO;
    timerCollection.showsHorizontalScrollIndicator = NO;
    timerCollection.showsVerticalScrollIndicator = NO;
    timerCollection.backgroundColor = [UIColor whiteColor];
    return timerCollection;
    
}


#pragma mark -collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return arrTimerDays.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"timerDayCell";
    STTimerDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    NSDictionary * dic = arrTimerDays[indexPath.row];
    [cell setOwnCollectionCellWithData:dic];
//    cell.dayBtn.enabled =isEnable;
    if (!isEnable) {
        [cell.dayBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//        cell.dayBtn.layer.borderColor =[UIColor lightGrayColor].class
    }else{
        [cell.dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(isEnable){
        STTimerDayCell *cell = (STTimerDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.dayBtn.backgroundColor = [UIColor orangeColor];
        cell.isSel = 0;
        [cell actionChangeDayBtn];
        [self showColListSelRow:indexPath];
    }
   
}

//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    STTimerDayCell *cell = (STTimerDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.dayBtn.backgroundColor = [UIColor whiteColor];
//    [cell.dayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    //    cell.dayBtn.selected = NO;
//}

//collection 回调
-(void)showColListSelRow:(NSIndexPath *)row{
//    if(row.row == 2){
        if ([self.delegate respondsToSelector:@selector(timerMagSelAutoDayTimer)]) {
            [self.delegate timerMagSelAutoDayTimer];
        }
//    }
    
}

@end
